# RedFire Backend Makefile
# =======================

# 变量定义
DOCKER_COMPOSE = docker-compose
DOCKER_COMPOSE_PROD = docker-compose -f docker-compose.prod.yml
PYTHON = python3
PIP = pip3

# 默认目标
.DEFAULT_GOAL := help

# ===================
# 帮助信息
# ===================

.PHONY: help
help: ## 显示帮助信息
	@echo "RedFire Backend - 可用命令:"
	@echo ""
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "  \033[36m%-20s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
	@echo ""

# ===================
# 环境设置
# ===================

.PHONY: install
install: ## 安装Python依赖
	$(PIP) install -r requirements.txt

.PHONY: install-dev
install-dev: ## 安装开发依赖
	$(PIP) install -r requirements.txt
	$(PIP) install -r requirements-dev.txt

.PHONY: venv
venv: ## 创建虚拟环境
	python3 -m venv venv
	@echo "请运行: source venv/bin/activate (Linux/Mac) 或 venv\\Scripts\\activate (Windows)"

# ===================
# 开发环境
# ===================

.PHONY: dev-up
dev-up: ## 启动开发环境
	$(DOCKER_COMPOSE) up -d

.PHONY: dev-down
dev-down: ## 停止开发环境
	$(DOCKER_COMPOSE) down

.PHONY: dev-restart
dev-restart: ## 重启开发环境
	$(DOCKER_COMPOSE) restart

.PHONY: dev-logs
dev-logs: ## 查看开发环境日志
	$(DOCKER_COMPOSE) logs -f

.PHONY: dev-build
dev-build: ## 构建开发环境镜像
	$(DOCKER_COMPOSE) build --no-cache

# ===================
# 基础设施服务
# ===================

.PHONY: infra-up
infra-up: ## 启动基础设施服务(数据库、Redis、RabbitMQ)
	$(DOCKER_COMPOSE) up -d postgres redis rabbitmq

.PHONY: infra-down
infra-down: ## 停止基础设施服务
	$(DOCKER_COMPOSE) stop postgres redis rabbitmq

.PHONY: infra-logs
infra-logs: ## 查看基础设施服务日志
	$(DOCKER_COMPOSE) logs -f postgres redis rabbitmq

# ===================
# 单个服务管理
# ===================

.PHONY: start-gateway
start-gateway: ## 启动API网关
	$(DOCKER_COMPOSE) up -d api_gateway

.PHONY: start-user_service
start-user_service: ## 启动用户服务
	$(DOCKER_COMPOSE) up -d user_service

.PHONY: start-trading-service
start-trading-service: ## 启动交易服务
	$(DOCKER_COMPOSE) up -d trading_service

.PHONY: start-market-service
start-market-service: ## 启动市场数据服务
	$(DOCKER_COMPOSE) up -d market_data_service

.PHONY: start-notification-service
start-notification-service: ## 启动通知服务
	$(DOCKER_COMPOSE) up -d notification_service

.PHONY: start-monitoring-service
start-monitoring-service: ## 启动监控服务
	$(DOCKER_COMPOSE) up -d monitoring_service

.PHONY: start-all-services
start-all-services: ## 启动所有微服务
	$(DOCKER_COMPOSE) up -d api_gateway user_service trading_service market_data_service notification_service monitoring_service

# ===================
# 本地开发 (统一入口)
# ===================

.PHONY: dev-start
dev-start: ## 开发模式启动所有服务 (本地Python)
	@echo "🚀 启动RedFire Backend微服务 (开发模式)..."
	$(PYTHON) main.py

.PHONY: dev-service
dev-service: ## 启动指定服务 (使用: make dev-service SERVICE=user_service)
	@echo "🚀 启动指定服务: $(SERVICE)"
	$(PYTHON) main.py --service $(SERVICE)

.PHONY: dev-health
dev-health: ## 本地开发模式健康检查
	$(PYTHON) main.py --health

.PHONY: dev-status
dev-status: ## 查看本地服务状态
	$(PYTHON) main.py --status

.PHONY: dev-urls
dev-urls: ## 显示服务访问地址
	$(PYTHON) main.py --urls

# ===================
# 单服务本地运行
# ===================

.PHONY: run-gateway
run-gateway: ## 本地运行API网关
	cd services/api_gateway && $(PYTHON) app/main.py

.PHONY: run-user_service
run-user_service: ## 本地运行用户服务
	cd services/user_service && $(PYTHON) app/main.py

.PHONY: run-trading-service
run-trading-service: ## 本地运行交易服务
	cd services/trading_service && $(PYTHON) app/main.py

# ===================
# 数据库管理
# ===================

.PHONY: db-migrate
db-migrate: ## 执行数据库迁移
	$(PYTHON) scripts/database/migrate.py

.PHONY: db-reset
db-reset: ## 重置数据库
	$(DOCKER_COMPOSE) stop postgres
	$(DOCKER_COMPOSE) rm -f postgres
	docker volume rm redfire_postgres_data || true
	$(DOCKER_COMPOSE) up -d postgres
	sleep 10
	$(MAKE) db-migrate

.PHONY: db-backup
db-backup: ## 备份数据库
	$(PYTHON) scripts/database/backup.py

.PHONY: db-shell
db-shell: ## 连接数据库shell
	$(DOCKER_COMPOSE) exec postgres psql -U redfire -d redfire

# ===================
# 测试
# ===================

.PHONY: test
test: ## 运行所有测试
	$(PYTHON) -m pytest tests/ -v

.PHONY: test-unit
test-unit: ## 运行单元测试
	$(PYTHON) -m pytest tests/unit/ -v

.PHONY: test-integration
test-integration: ## 运行集成测试
	$(PYTHON) -m pytest tests/integration/ -v

.PHONY: test-e2e
test-e2e: ## 运行端到端测试
	$(PYTHON) -m pytest tests/e2e/ -v

.PHONY: test-coverage
test-coverage: ## 运行测试覆盖率
	$(PYTHON) -m pytest --cov=services tests/ --cov-report=html

# ===================
# 代码质量
# ===================

.PHONY: lint
lint: ## 代码检查
	flake8 services/ shared/ scripts/
	black --check services/ shared/ scripts/
	isort --check-only services/ shared/ scripts/

.PHONY: format
format: ## 格式化代码
	black services/ shared/ scripts/
	isort services/ shared/ scripts/

.PHONY: type-check
type-check: ## 类型检查
	mypy services/ shared/ scripts/

# ===================
# 监控和日志
# ===================

.PHONY: monitoring-up
monitoring-up: ## 启动监控服务
	$(DOCKER_COMPOSE) up -d prometheus grafana

.PHONY: monitoring-down
monitoring-down: ## 停止监控服务
	$(DOCKER_COMPOSE) stop prometheus grafana

.PHONY: logs
logs: ## 查看所有服务日志
	$(DOCKER_COMPOSE) logs -f

.PHONY: logs-gateway
logs-gateway: ## 查看API网关日志
	$(DOCKER_COMPOSE) logs -f api_gateway

.PHONY: logs-user
logs-user: ## 查看用户服务日志
	$(DOCKER_COMPOSE) logs -f user_service

# ===================
# 生产环境
# ===================

.PHONY: prod-up
prod-up: ## 启动生产环境
	$(DOCKER_COMPOSE_PROD) up -d

.PHONY: prod-down
prod-down: ## 停止生产环境
	$(DOCKER_COMPOSE_PROD) down

.PHONY: prod-build
prod-build: ## 构建生产环境镜像
	$(DOCKER_COMPOSE_PROD) build --no-cache

.PHONY: prod-logs
prod-logs: ## 查看生产环境日志
	$(DOCKER_COMPOSE_PROD) logs -f

.PHONY: prod-deploy
prod-deploy: ## 部署到生产环境
	$(PYTHON) scripts/deployment/deploy.py --environment production

# ===================
# 清理
# ===================

.PHONY: clean
clean: ## 清理Docker资源
	docker system prune -f
	docker volume prune -f
	docker network prune -f

.PHONY: clean-all
clean-all: ## 清理所有Docker资源(包括镜像)
	$(DOCKER_COMPOSE) down -v --rmi all
	docker system prune -af
	docker volume prune -f

# ===================
# 健康检查
# ===================

.PHONY: health
health: ## 检查所有服务健康状态
	$(PYTHON) scripts/utilities/health_check.py

.PHONY: status
status: ## 显示服务状态
	$(DOCKER_COMPOSE) ps

# ===================
# 文档
# ===================

.PHONY: docs
docs: ## 生成API文档
	$(PYTHON) scripts/utilities/generate_docs.py

.PHONY: docs-serve
docs-serve: ## 启动文档服务器
	cd docs && $(PYTHON) -m http.server 8080

# ===================
# 工具
# ===================

.PHONY: shell
shell: ## 进入开发容器shell
	$(DOCKER_COMPOSE) exec api_gateway /bin/bash

.PHONY: redis-cli
redis-cli: ## 连接Redis CLI
	$(DOCKER_COMPOSE) exec redis redis-cli

.PHONY: rabbitmq-mgmt
rabbitmq-mgmt: ## 打开RabbitMQ管理界面
	@echo "RabbitMQ管理界面: http://localhost:15672"
	@echo "用户名: redfire"
	@echo "密码: redfire123"

.PHONY: grafana-dashboard
grafana-dashboard: ## 打开Grafana仪表板
	@echo "Grafana仪表板: http://localhost:3000"
	@echo "用户名: admin"
	@echo "密码: admin123"

# ===================
# 快速启动
# ===================

.PHONY: quick-start
quick-start: ## 快速启动完整环境
	@echo "🚀 启动RedFire Backend..."
	$(MAKE) infra-up
	@echo "⏳ 等待基础设施服务启动..."
	sleep 15
	$(MAKE) start-all-services
	@echo "⏳ 等待微服务启动..."
	sleep 10
	$(MAKE) monitoring-up
	@echo "✅ RedFire Backend启动完成!"
	@echo ""
	@echo "🌐 服务地址:"
	@echo "  - API网关: http://localhost:8000"
	@echo "  - 用户服务: http://localhost:8001"
	@echo "  - 交易服务: http://localhost:8002"
	@echo "  - 市场数据: http://localhost:8003"
	@echo "  - 通知服务: http://localhost:8004"
	@echo "  - 监控服务: http://localhost:8005"
	@echo "  - Grafana: http://localhost:3000"
	@echo "  - RabbitMQ: http://localhost:15672"

.PHONY: quick-stop
quick-stop: ## 快速停止所有服务
	@echo "🛑 停止RedFire Backend..."
	$(DOCKER_COMPOSE) down
	@echo "✅ 所有服务已停止!"

# ===================
# 开发工具
# ===================

.PHONY: create-service
create-service: ## 创建新的微服务 (使用: make create-service NAME=service-name)
	$(PYTHON) scripts/utilities/create_service.py $(NAME)

.PHONY: update-deps
update-deps: ## 更新依赖
	$(PIP) freeze > requirements.txt
